package com.lkd.http.controller;
import com.lkd.common.VMSystem;
import com.lkd.entity.TaskEntity;
import com.lkd.exception.LogicException;
import com.lkd.http.vo.*;
import com.lkd.service.*;
import com.lkd.vo.Pager;
import org.apache.ibatis.logging.LogException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/task")
public class TaskController extends  BaseController {
    @Autowired
    private TaskService taskService;

    //创建工单
    @PostMapping("/create")
    public boolean create(@RequestBody TaskViewModel tvm) {
        return taskService.productionOrderSave(tvm);
    }

    //接受工单
    @GetMapping("/accept/{id}")
    public void accept(@PathVariable("id") Long id) {
        TaskEntity task = taskService.getById(id);
        //如果当前登录用户不是工单执行人，则抛出异常
        if (!getUserId().equals(task.getUserId())) {
            try {
                throw new Exception("登录用户不是工单执行人");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        taskService.accept(task);
    }
    //取消工单
    @PostMapping("/cancel/{id}")
    public  boolean cancelTask(@PathVariable("id") Long id,@RequestBody CancelTaskViewModel ctvm){
        TaskEntity task = taskService.getById(id);
        //如果当前登录用户不是工单执行人，则抛出异常
        if (!getUserId().equals(task.getUserId())) {
                throw new LogException("登录用户不是工单执行人");
        }
    return     taskService.cancelTask(task,ctvm);
    }
    //完成工单
    @GetMapping("/complete/{id}")
    public  boolean completeTask(@PathVariable("id") Long id){
        TaskEntity task = taskService.getById(id);
        //如果当前登录用户不是工单执行人，则抛出异常
        if (!getUserId().equals(task.getUserId())) {
            throw new LogException("登录用户不是工单执行人");
        }
        return     taskService.finishTask(task);
    }

    /**
     * 搜索工单
     * @param pageIndex
     * @param pageSize
     * @param innerCode 设备编号
     * @param userId  工单所属人Id
     * @param taskCode 工单编号
     * @param status 工单状态
     * @param isRepair 是否是维修工单
     * @return
     */
    @GetMapping("/search")
    public Pager<TaskEntity> search(
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
            @RequestParam(value = "innerCode",required = false,defaultValue = "") String innerCode,
            @RequestParam(value = "userId",required = false,defaultValue = "") Integer userId,
            @RequestParam(value = "taskCode",required = false,defaultValue = "") String taskCode,
            @RequestParam(value = "status",required = false,defaultValue = "") Integer status,
            @RequestParam(value = "isRepair",required = false,defaultValue = "") Boolean isRepair,
            @RequestParam(value = "start",required = false,defaultValue = "") String start,
            @RequestParam(value = "end",required = false,defaultValue = "") String end){
        return taskService.search(pageIndex,pageSize,innerCode,userId,taskCode,status,isRepair,start,end);
    }

    //工单详情
}